package com.zs.propertymanage.service.impl.impl;

import com.zs.propertymanage.dataObj.DTO;
import com.zs.propertymanage.dataObj.GeneralExtra;
import com.zs.propertymanage.dataObj.R;
import com.zs.propertymanage.dataObj.entity.House;
import com.zs.propertymanage.dataObj.entity.Org;
import com.zs.propertymanage.mapper.HouseMapper;
import com.zs.propertymanage.mapper.OrgMapper;
import com.zs.propertymanage.service.impl.HouseService;
import com.zs.propertymanage.service.impl.OrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HouseServiceImpl implements HouseService {

    @Autowired
    private HouseMapper houseMapper;
    @Autowired
    private OrgMapper orgMapper;
    @Autowired
    private OrgService orgService;

    /**
     * 根据搜索参数、分页、组织 查询房屋列表
     * 1. 首先获取所有组织
     * 2. 根据传入的组织id，获取该组织及其子组织列表
     * 3. 查询属于这些组织的房屋列表
     *
     * @param search      搜索参数
     * @param currentPage 当前页
     * @param pageSize    每页显示的记录数
     * @param orgId       组织id
     * @return 查询结果
     */
    @Override
    public R getHouse(String search, Integer currentPage, Integer pageSize, Integer orgId) {
        // 1. 获取该组织及所有子组织id列表
        List<Integer> resultOrgList = orgService.getChildrenIdList(orgId);

        // 2. 查询属于这些组织的房屋列表
        List<House> resultHouseList = houseMapper.getHouseByOrgIdList(search, (currentPage - 1) * pageSize, pageSize, resultOrgList, true);
        Integer total = houseMapper.getHouseCountByOrgIdList(search, resultOrgList);

        return total > 0 ? R.ok("查询成功", new DTO<>(resultHouseList, total)) : R.error("未查询到房屋数据");
    }

    @Override
    public R addHouse(House house) {
        if (house.getOrgId() == null) {
            return R.error("所属组织不能为空");
        }
        Integer result = houseMapper.addHouse(setHousePath(house));
        return result > 0 ? R.ok("添加房屋信息成功") : R.error("添加房屋信息失败");
    }

    @Override
    public R updateHouse(House house) {
        Integer result = houseMapper.updateHouse(setHousePath(house));
        return result > 0 ? R.ok("更新房屋信息成功") : R.error("更新房屋信息失败");
    }

    /**
     * 设置房屋路径
     * 所属组织路径-房屋名称
     *
     * @param house 房屋信息
     * @return 添加完路径的房屋信息
     */
    private House setHousePath(House house) {
        Org org = orgMapper.getOrgById(house.getOrgId());
        house.setPath(org.getPath() + "-" + house.getName());
        return house;
    }

    @Override
    public R deleteHouse(Integer id) {
        Integer result = houseMapper.deleteHouse(id);
        return result > 0 ? R.ok("删除房屋信息成功") : R.error("删除房屋信息失败");
    }

    @Override
    public R getAllHouseType() {
        List<GeneralExtra> list = houseMapper.getAllHouseType();
        return list.size() > 0 ? R.ok("获取房屋类型成功", list) : R.error("获取房屋类型失败");
    }

    @Override
    public R getAllHouseStatus() {
        List<GeneralExtra> list = houseMapper.getAllHouseStatus();
        return list.size() > 0 ? R.ok("获取房屋状态成功", list) : R.error("获取房屋状态失败");
    }

    @Override
    public R addHouseType(String typeName) {
        if (typeName == null || typeName.equals(""))
            return R.error("添加失败，房屋类型名称不能为空");
        Integer result = houseMapper.addHouseType(typeName);
        return result > 0 ? R.ok("添加房屋类型成功") : R.error("添加房屋类型失败");
    }

    @Override
    public R deleteHouseType(Integer id) {
        Integer result = houseMapper.deleteHouseType(id);
        return result > 0 ? R.ok("删除房屋类型成功") : R.error("删除房屋类型失败");
    }

    @Override
    public R addHouseStatus(String statusName) {
        if (statusName == null || statusName.equals(""))
            return R.error("添加失败，房屋状态名称不能为空");
        Integer result = houseMapper.addHouseStatus(statusName);
        return result > 0 ? R.ok("添加房屋状态成功") : R.error("添加房屋状态失败");
    }

    @Override
    public R deleteHouseStatus(Integer id) {
        Integer result = houseMapper.deleteHouseStatus(id);
        return result > 0 ? R.ok("删除房屋状态成功") : R.error("删除房屋状态失败");
    }
}
